<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 网站用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>
<form class="layui-form">
    <input type="hidden" name="id" th:value="${user.id}">
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-inline">
            <input th:value="${user.name}" type="text" name="name" lay-verify="required" lay-reqtext="用户名是必填项，岂能为空？" placeholder="请输入"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <input name="oldpwd" type="hidden" th:value="${user.password}">
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input th:value="${user.password}" type="password" name="password" lay-verify="pass" placeholder="请输入密码" autocomplete="off"
                   class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">请填写密码</div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">联系电话</label>
        <div class="layui-input-inline">
            <input type="text" name="phone" th:value="${user.phone}" lay-verify="phone"  placeholder="请输入用户联系方式"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">地址</label>
        <div class="layui-input-inline">
            <input type="text" name="address" th:value="${user.address}" lay-verify="required"  placeholder="请输入用户地址"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">请选择用户角色</label>
        <div class="layui-input-block">
            <input th:each="role:${roles}" th:checked="${roleIdList.contains(role.id)}" type="checkbox" th:name="roles" th:value="${role.id}" th:title="${role.nameZh}">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否启用</label>
        <div class="layui-input-block">
            <input type="checkbox" th:checked="${user.enabled}" name="enabled" lay-skin="switch" lay-text="ON|OFF">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否锁定</label>
        <div class="layui-input-block">
            <input type="checkbox" th:checked="${user.locked}" name="locked" lay-skin="switch" lay-text="ON|OFF">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1" id="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script src="/static/layuiadmin/layui/layui.js"></script>

<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , $ = layui.$;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            /*/^[\S]{6,12}$/*/
            , pass: function (value) {
                var reg=/^[\S]{6,12}$/;
                var oldpwd=$("[name=oldpwd]").val();
                if(value!==oldpwd){
                    if (!reg.test(value)) {
                        return '修改的密码必须6到12位，且不能出现空格';
                    }
                }
            }
            , phone: [
                /^1[3|4|5|8]\d{9}$/
                , '请输入正确的电话号码'
            ]
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        form.on('submit(demo1)', function (data) {
            var arr = new Array();
            $("input:checkbox[name='roles']:checked").each(function(i){
                arr[i] = $(this).val();
            });
            data.field.roles = arr.join(",");
            data.field.enabled = data.field.enabled ? "on": "off"
            data.field.locked = data.field.locked ? "on": "off"
            $.ajax({
                url: '/doupdate'
                , method: 'post'
                , data: data.field
                , success: function (res) {
                    layer.msg("修改成功");
                    setTimeout(function () {
                        window.parent.location.reload();//修改成功后刷新父界面
                    }, 1000);
                }
            });
            return false;
        });


    })
    ;
</script>
</html>